﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

//有多种可选功能，均不是必须的
[RequireComponent(typeof(BoxCollider2D))]
public class Device : MonoBehaviour
{
    private CameraController cameraController;
    public Fan windArea;            //要激活(关闭)的气流
    public CameraMove cameraMove;        
    public GameObject obstacle;     //要摧毁的障碍物
    public Checkpoint checkPoint;   //要重生到的记录点

    [SerializeField]
    protected bool b_touchPlayer;
    protected bool b_interactable = true;   //能否与玩家互动

    [SerializeField]
    protected bool _Active; //是否激活
    public bool Active
    {
        get => _Active;
        set
        {
            if (_Active == value)
                return;
            _Active = value;
            OnActivate(value);
        }
    }

    private void Awake()
    {
        cameraController = Camera.main.GetComponentInChildren<CameraController>();
    }

    protected void Update()
    {
        OnUpdate();
    }

    protected virtual void OnUpdate()
    {  
        if(b_touchPlayer&& b_interactable)
        {
            if(Input.GetKeyDown(KeyCode.S))
            {
                Active = true;
            }
        }
    }

    private void OnTriggerEnter2D(Collider2D collision)
    {
        if(collision.GetComponent<Character>() != null)
        {
            b_touchPlayer = true;
            OnTouchPlayer();
        }
    }

    private void OnTriggerExit2D(Collider2D collision)
    {
        if (collision.GetComponent<Character>() != null)
        {
            b_touchPlayer = false;
            OnLeavePlayer();
        }
    }

    protected virtual void OnActivate(bool value)
    {
        b_interactable = false;
        if (windArea != null)
            windArea.Active = !windArea.Active;
        if (cameraMove != null && cameraMove.singleCameraMoves.Count != 0)
            cameraController.StartMove(cameraMove);
        if (obstacle != null)
            Destroy(obstacle);
        if (checkPoint != null)
            checkPoint.Spawn();
    }
    protected virtual void OnTouchPlayer() { }
    protected virtual void OnLeavePlayer() { }
}
